package com.mmy.controller;

import java.io.File;
import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;

public class FileUtils {
	/**
	 *
	 * @param file     文件
	 * @param path     文件存放路径
	 * @param fileName 源文件名
	 * @return
	 */
	public static String upload(MultipartFile file, String path) {
		
		String local = null;
		
		if(file != null) {
			String cadeName = file.getOriginalFilename();
			String newCadeName = FileNameUtils.getFileName(cadeName);
			String realPath = path + "/" + newCadeName;
			local = "imges/"+ newCadeName;
			File dest = new File(realPath);
			if (!dest.getParentFile().exists()) {
				dest.getParentFile().mkdir();
			}
			try {
				file.transferTo(dest);
			} catch (IllegalStateException e) {
				e.printStackTrace();
				return null;
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		return local;
	}
}
